package com.aust.furniture.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.aust.furniture.common.ResultMapUtil;
import com.aust.furniture.entity.Furniture;
import com.aust.furniture.service.IFurnitureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 家具相关的controller
 */
@Controller
@RequestMapping(value = "/furniture")
public class FurnitureController {

    @Autowired
    private IFurnitureService furnitureService;

    /**
     * 转向家具页面
     */
    @RequestMapping
    public String furniture(){
        return "/furniture";
    }

    /**
     * 分页查询家具列表
     */
    @RequestMapping(value = "/furnitureQueryPage")
    @ResponseBody
    public Object furnitureQueryPage(String param, @RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int limit){
        try{
            IPage<Furniture> iPage = furnitureService.selectFurniturePage(page,limit,param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向家具新增页面
     */
    @RequestMapping(value = "/furniturePage")
    public String furniturePage(){
        return "/furniturePage";
    }

    /**
     * 添加一个家具
     */
    @RequestMapping(value = "/furnitureAdd")
    @ResponseBody
    public Object furnitureAdd(Furniture furniture){
        try{
            int i = furnitureService.addFurniture(furniture);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向家具编辑页面
     */
    @RequestMapping(value = "/furnitureQueryById")
    public String furnitureQueryById(@RequestParam(name = "id",required = true)Integer id, Model model){
        Furniture furniture = furnitureService.queryFurnitureById(id);
        model.addAttribute("obj",furniture);
        return "/furniturePage";
    }

    /**
     * 修改一个家具
     */
    @RequestMapping(value = "/furnitureEdit")
    @ResponseBody
    public Object furnitureEdit(Furniture furniture){
        try{
            int i = furnitureService.editFurniture(furniture);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个家具
     */
    @RequestMapping(value = "/furnitureDelById")
    @ResponseBody
    public Object furnitureDelById(Integer id){
        try{
            int i = furnitureService.delFurnitureById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }
    //   获取所有家具
    @RequestMapping(value = "/furnitureList")
    @ResponseBody
    public Object furnitureList(){
        List<Furniture> furnitureList = furnitureService.queryFurnitureList();
        return ResultMapUtil.getHashMapList(furnitureList);
    }

    /**
     * 转向家具保质期检查页面
     */
    @RequestMapping(value = "/warranty")
    public String warranty(){
        return "/warranty";
    }

}





















